#include "iotgo_mySocketClient_interface.h"
#include "iotgo_config.h"
#include "iotgo_platformAPI.h"
#include "iotgo_debug.h"
#include "gprs_command.h"

#if (GPRS_ON)

#define debugPrint socketClientDebugPrint

#define MAX_DOMAIN_LEN 20

struct mySocketClient_interface
{
    uint8_t  server_addr[20];
    uint32_t server_port;
    int8_t connect_channel;
};

static uint8_t gprsFail_count = 0;

MySocketClient *setupSocketClient(uint8_t *server_ip,uint32_t server_port)
{
    MySocketClient *p_socket = NULL;
	
    p_socket = (MySocketClient *)malloc(sizeof(MySocketClient));

    if(!p_socket)
    {
	return NULL;
    }

    memset(p_socket,0,sizeof(MySocketClient));
    strcpy(p_socket->server_addr,server_ip);
    p_socket->server_port = server_port;

    return p_socket;
}

int8_t cleanSocketClient(MySocketClient **pp_mysocket)
{
    MySocketClient *p_mysocket = NULL;
    p_mysocket = *pp_mysocket;
    if (p_mysocket)
    {
	gprsCloseCon(p_mysocket->connect_channel);
	free(p_mysocket);
    }
    return MYSOCKETCLIENT_SUCCESS;
}

int8_t connectToServer(MySocketClient *p_mysocket)
{
    int8_t ret = -1;

    ret = gprsCheckMuxCon();
    if(ret != 1)
    {
	ret = gprsSetMuxCon();
	if(ret != 0)
	{
	    printf("[%s] fail to set mux con\n",__FUNCTION__);
	    gprsFail_count++;
	    if(gprsFail_count == 5)
	    {
		gprsReset();
		gprsFail_count = 0;
	    }
	    return MYSOCKETCLIENT_FAIL;
	}
    }

    ret = gprsStartTCP(p_mysocket->server_addr,p_mysocket->server_port);
    printf("[%s] gprsStartTcp ret is [%d] [%c]\n",__FUNCTION__,ret,ret);
    if(ret >= 0)
    {
	p_mysocket->connect_channel = ret;
	gprsCheckCon();
	return MYSOCKETCLIENT_SUCCESS;
    }
    else
    {
	return MYSOCKETCLIENT_FAIL;
    }

}

int32_t readFromServer(MySocketClient *p_mysocket, uint8_t *buf, uint32_t len)
{
    int32_t ret = 0;
    int8_t channel = 0;
    uint8_t count = 0;
    while(1)
    {
	ret = gprsTCPreadMux(p_mysocket->connect_channel,buf,len);
	if(ret > 0)
	{
	    printf("[%s] ret is [%d]\n",__FUNCTION__,ret);
	    return ret;
	}
	else if(ret == GPRS_CON_LOST)
	{
	    printf("[%s] con lost\n",__FUNCTION__);
	    return MYSOCKETCLIENT_CLOSED;
	}
	else
	{
    	    //printf("[%s] systemDelayMs. ret is  [%d]\n",__FUNCTION__,ret);
	    systemDelayMs(100);
	    count++;
	    if(count == 50)
	    {
		return MYSOCKETCLIENT_OVERTIME;
	    }
	}
    }
}

int32_t readFromServerNonBlock(MySocketClient *p_mysocket,uint8_t *buf, uint32_t len)
{
    int32_t ret;
    int8_t channel = 0;
    ret = gprsTCPread(&channel,buf,len);
    return ret;
}


int32_t writeToServer(MySocketClient *p_mysocket, uint8_t* buf, uint32_t len)
{
    int32_t ret = -1;


    ret = gprsTCPsendMux(p_mysocket->connect_channel,buf,len);
    while(ret == -3)
    {
	printf("[%s] gprsTCPsendMux ret [%d]\n",__FUNCTION__,ret);
	systemDelayMs(100);
	ret = gprsTCPsendMux(p_mysocket->connect_channel,buf,len);
    }

    return ret;
}

int32_t writeToServerDebug(MySocketClient *p_mysocket,uint8_t *buf, uint32_t len)
{
#if 0
    int32_t ret = -1;
    printf("[%s] channel is [%d]\n",__FUNCTION__,p_mysocket->connect_channel);
    ret = gprsTCPsendMuxDebug(p_mysocket->connect_channel,buf,len);
    return ret;
#endif
    return -1;
}

#endif
